//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    inputValue: '', //搜索的内容
    // 轮播
    imgUrls: [
        '../../images/banner01.png',    
        '../../images/banner02.png',
        '../../images/banner03.png',  
    ],
    circular: true,
    //是否显示画板指示点 
    indicatorDots: true,
    //选中点的颜色 
    //是否竖直 
    vertical: false,
    //是否自动切换 
    autoplay: true,
    //自动切换的间隔
    interval: 3000,
    //滑动动画时长毫秒 
    duration: 1000,
    //所有图片的高度 
    imgheights: [],
    //图片宽度 
    imgwidth: 750,
    //默认 
    current: 0,
    listData: [
      { "code": "员工A", "text": "35" },
      { "code": "员工B", "text": "34" },
      { "code": "员工C", "text": "30"},
      { "code": "员工D", "text": "25"},
      { "code": "员工E", "text": "24"},
      { "code": "员工F", "text": "23"},
    ],
    showView: true,


    
  },
  /**
  * 判断授权点击接受还是拒绝
  */
  bindGetUserInfo: function (e) {
    if (e.detail.userInfo) { // 用户按了允许授权按钮
      this.setData({ showView: false }) // 关闭授权弹窗
      app.wx_login(); // 用户登陆
      wx.showNavigationBarLoading(); // 开始顶部加载动画
      wx.showLoading({ title: 'CRM加载中' }, 2000) // 开始加载动画
    } else { // 用户按了拒绝按钮

    }
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  goStaffTap:function(){
    wx.navigateTo({
      url: '../staff/staff',
    })
  },
  goSurveyTap:function(event){
    console.log(event)
    var that = this;
    var uid = event.currentTarget.dataset.id;
    var name = event.currentTarget.dataset.name;
    wx.navigateTo({
      url: '../survey/survey?id=' + uid + '&name=' + name,
    })
  },
  imageLoad: function (e) {//获取图片真实宽度 
    var imgwidth = e.detail.width,
      imgheight = e.detail.height,
      //宽高比 
      ratio = imgwidth / imgheight;
    console.log(imgwidth, imgheight)
    //计算的高度值 
    var viewHeight = 750 / ratio;
    var imgheight = viewHeight;
    var imgheights = this.data.imgheights;
    //把每一张图片的对应的高度记录到数组里 
    imgheights[e.target.dataset.id] = imgheight;
    this.setData({
      imgheights: imgheights
    })
  },
  bindchange: function (e) {
    // console.log(e.detail.current)
    this.setData({ current: e.detail.current })
  },



  //搜索框文本内容显示
  inputBind: function (event) {
    this.setData({
      inputValue: event.detail.value
    })
    console.log('bindInput' + this.data.inputValue)

  },
  onLoad: function () {
    //列表
    var that =this;
    wx.request({
      url: 'https://data1.cqhot.cn/index.php/Admin/Apiwx/index',
      success: function (res) {
        console.log(res)
        if(res.data.code==1){
          that.setData({
            listData: res.data.data
          })
        }else{
          console.log("网络失败，数据未成功调取");
        }

        
      }
    })


    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },

  // 搜索接口备用
  /*    
  query: function (event) {

    var that = this

      /**
     * 提问帖子搜索API
     * keyword string 搜索关键词 ; 这里是 this.data.inputValue
     * start int 分页起始值 ; 这里是 0
    

    wx.request({
      url: 'https://localhost/proj_online_class/server/public/index.php/forum/forum/get_issue_search/' + this.data.inputValue + /0/,
      data: {
        inputValue: this.data.inputValue
      },
      method: 'GET',
      success: function (res) {
        console.log(res.data)
        var searchData = res.data
        that.setData({
          searchData
        })

        wx.setStorage({
          key: 'searchLists',
          data: {
            searchLists: res.data
          }
        })

        if (!that.data.inputValue) {
          //没有搜索词 友情提示
          wx.showToast({
            title: '请重新输入',
            image: '../../picture/tear.png',
            duration: 2000,
          })
        } else if (searchData.search.length == 0) {
          //搜索词不存在 友情提示
          wx.showToast({
            title: '关键词不存在',
            image: '../../picture/tear.png',
            duration: 2000,
          })
        } else {
          //提取题目关键字 与搜索词进行匹配
          var searchIndex = searchData.search.length
          var d = 0;
          for (var i = 0; i <= searchIndex - 1; i++) {

            var searchTitle = searchData.search[d].title
            console.log(searchTitle)
            d = d + 1;

            for (var x = 0; x <= searchTitle.length; x++) {
              for (var y = 0; y <= searchTitle.length; y++) {
                var keyWord = searchTitle.substring(x, y);
                console.log(keyWord)
              }
            }

            wx.navigateTo({
              url: '../search/search',
            })
          }
        }



      }
    })
  },
 */
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  onShareAppMessage: function (res) {
    return {
      title: 'CRM助手',
      path: '/pages/index/index',
      success: function () { },
      fail: function () { }
    }
  },
})
